import Vue from 'vue'
import App from './App'
import storeVuex from './storeVuex/index'
import { VueJsonp } from 'vue-jsonp'
Vue.use(VueJsonp)

import '@/utils/core.js'

// 此处为引用自定义顶部
import cuCustom from './colorui/components/cu-custom.vue'
Vue.component('cu-custom', cuCustom)
import cuCustomApp from './colorui/components/cu-custom-app.vue'
Vue.component('cu-custom-app', cuCustomApp)
import TnCustom from './components/TnCustom/TnCustom.vue'
Vue.component('tn-custom', TnCustom)

// 引入:uView-UI
import uView from 'uview-ui'
Vue.use(uView)

Vue.config.productionTip = false

App.mpType = 'app'

// #ifdef H5
import '@/static/font/iconfont.js'
import '@/static/font/iconfont.css'
document.write('<script type="text/javascript" src="/static/js/jweixin-1.4.0.js""></script>')
document.write('<script type="text/javascript" src="/static/js/uni.webview.1.5.2.js"></script>')
document.write('<script type="text/javascript" src="/static/js/h5player.min.js"></script>')
// // 写入app配置
// document.write('<script type="text/javascript" src="/static/app.config.js""></script>')
// // 写入map配置
// document.write('<script type="text/javascript" src="/static/config.js""></script>')
// #endif

// #ifdef H5
// 和core.js 相对应
window.getLocationNum = 0

window.receiveData = (latlon) => {
	let pos = latlon.split(',')
	uni.setStorageSync('latlon', {
		latitude: pos[0],
		longitude: pos[1]
	})
	getLocationNum++
}
// #endif

// #ifdef APP-PLUS
import Config from './config'
import store from './store'
import VueI18n from 'vue-i18n'
import forward from './common/forward'
import picker from './common/picker'
import wfc from './wfc/client/wfc'
import wfcUIKit from './wfc/uikit/wfcUIKit'
import { getItem } from './pages/util/storageHelper'
Vue.use(VueI18n)
Vue.use(picker)
Vue.use(forward)
const i18n = new VueI18n({
	// 使用localStorage存储语言状态是为了保证页面刷新之后还是保持原来选择的语言状态
	locale: getItem('lang') ? getItem('lang') : 'zh-CN', // 定义默认语言为中文
	messages: {
		'zh-CN': require('@/assets/lang/zh-CN.json'),
		'zh-TW': require('@/assets/lang/zh-TW.json'),
		en: require('@/assets/lang/en.json')
	}
})
// 如果不存在会话页面，则入栈，如果已经存在会话页面，则返回到该页面
Vue.prototype.$go2ConversationPage = () => {
	let pages = getCurrentPages()
	let cvRoute = 'pages/conversation/ConversationPage'
	let delta = 0
	let found = false
	for (let i = pages.length - 1; i >= 0; i--) {
		if (pages[i].route === cvRoute) {
			found = true
			break
		} else {
			delta++
		}
	}
	if (found) {
		uni.navigateBack({
			delta: delta,
			fail: (err) => {
				console.log('nav back to conversationView err', err)
			}
		})
	} else {
		uni.navigateTo({
			url: '/pages/conversation/ConversationPage',
			success: () => {
				console.log('nav to conversationPage success')
			},
			fail: (err) => {
				console.log('nav to conversationPage err', err)
			}
		})
	}
}
Vue.prototype.$scrollToBottom = () => {
	setTimeout(() => {
		uni.pageScrollTo({
			scrollTop: 999999,
			duration: 10
		})
		app.$forceUpdate()
	}, 100)
}
Vue.prototype.$avatar = ``

Vue.prototype._i18n = i18n
// #endif

const app = new Vue({
	store: storeVuex,
	...App,
	// #ifdef APP-PLUS
	i18n
	// #endif
})

// #ifdef APP-PLUS
app.store = store
wfc.init()
if (wfcUIKit.isUIKitEnable()) {
	Config.ICE_SERVERS.forEach((iceServer) => {
		wfcUIKit.addICEServer(iceServer.uri, iceServer.userName, iceServer.password)
	})
}
store.init()

// #endif

app.$mount()
